local gaishi = fk.CreateSkill {
  name = "kl__gaishi",
}

Fk:loadTranslationTable{
  ["kl__gaishi"] = "盖世",
  [":kl__gaishi"] = "出牌阶段限一次，你可以将至多X张手牌当一张不计次数的【杀】使用，若如此做，你选择一项："..
  "1.此【杀】伤害值调整为Y（Y为转化牌的数量）；2.此【杀】结算后摸X张牌（X为你的攻击范围且至多为四）。",
  ["#kl__gaishi"] = "盖世：将至多%arg张手牌当一张不计次数的【杀】使用，然后选择一项增益此杀",
  ["kl__gaishi-damage"] = "此【杀】伤害值调整为%arg",
  ["kl__gaishi-draw"] = "此【杀】结算后你摸%arg张牌",

  ["$kl__gaishi1"] = "乘赤免，舞画载，斩将破敌不过举手而为！",
  ["$kl__gaishi2"] = "此身此武，天下无双！",
}


gaishi:addEffect("viewas", {
  anim_type = "offensive",
  prompt = function(self, player)
    local num = math.min(4, player:getAttackRange())
    return "#kl__gaishi:::"..num
  end,
  pattern = "slash",
  handly_pile = true,
  min_card_num = 1,
  card_filter = function(self, player, to_select, selected)
    return #selected < player:getAttackRange() and table.contains(player:getHandlyIds(true), to_select)
  end,
  interaction = function(self, player)
    local all_names = table.filter(Fk:getAllCardNames("b"), function(name)
      return Fk:cloneCard(name).trueName == "slash"
    end)
    local names = player:getViewAsCardNames(gaishi.name, all_names)
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,
  before_use = function (self, player, use)
    local room = player.room
    use.extraUse = true
    local y = #use.card.subcards
    local x = math.min(4, player:getAttackRange())
    local ch = room:askToChoice(player,{
      choices = {"kl__gaishi-damage:::"..y, "kl__gaishi-draw:::"..x},
      skill_name = gaishi.name,
    })
    if ch:startsWith("kl__gaishi-damage") then
      if y < 2 then return end
      use.additionalDamage = (use.additionalDamage or 0) + y-1
    elseif ch:startsWith("kl__gaishi-draw") then
      if x < 1 then return end
      use.extra_data = use.extra_data or {}
      use.extra_data.kl__gaishi_draw = {
        player = player,
        x = x,
      }
    end
  end,
  view_as = function(self, player, cards)
    if self.interaction.data == nil or #cards < 1 then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = gaishi.name
    return card
  end,
  enabled_at_play = function (self, player)
    return player:hasSkill(gaishi.name) and player.phase == Player.Play and player:usedEffectTimes(self.name, Player.HistoryPhase) == 0
  end,
  enabled_at_response = function (self, player,response)
    return not response and player:usedEffectTimes(self.name, Player.HistoryPhase) == 0
  end,
})

gaishi:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and data.card and data.extra_data and data.extra_data.kl__gaishi_draw and data.extra_data.kl__gaishi_draw.player and data.extra_data.kl__gaishi_draw.player == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = data.extra_data.kl__gaishi_draw.x
    local who = data.extra_data.kl__gaishi_draw.player
    room:drawCards(who, x, gaishi.name)
  end,
})

gaishi:addEffect("targetmod", {
  bypass_times = function (self, player, skill, scope, card, to)
    return card and table.contains(card.skillNames, gaishi.name)
  end,
})


return gaishi
